﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using UTLDB;

namespace frmKiemKho
{
    public partial class frmKiemKho : UTLBASEFORM.MDICHILD
    {
        private DataSet dsKho;

        public frmKiemKho()
        {
            InitializeComponent();
        }

        private void frmKiemKho_Load(object sender, EventArgs e)
        {
            setupMenu();
            //dataGridView1.Rows[0].Cells[1].Value =(Bitmap) getImage(ImageNameDelete);
        }

        private void setupMenu()
        {
            ShowBTN1(TextLableCheck, "");
        }
        public override void btn1_Click(object sender, EventArgs e)
        {
            if (txtPathFile.Text.Equals(""))
            {
                return;
            }            
            CReadBarcode re = new CReadBarcode();
            re.PathFile = txtPathFile.Text;
            ArrayList _barcode = FilterDuplicatRecord(re.ReadBarCode());
            //lstBarcode.DataSource = _barcode;
            //dsKho = LoadData();
            //if (dsKho == null)
            //{
            //    MessageBox.Show(MsgNoAccessData, MsgTitile);
            //    return;
            //}
            //DataTable dt1 = new DataTable();
            //dt1 = dsKho.Tables[1];
            //if (dt1 == null)
            //{
            //    MessageBox.Show(MsgNoProductData, MsgTitile);
            //    return;
            //}
            //if (dt1.Rows.Count < 1)
            //{
            //    MessageBox.Show(MsgNoProductData, MsgTitile);
            //    return;
            //}
            //if (_barcode.Count<1)
            //{
            //    MessageBox.Show(MsgNoProductData, MsgTitile);
            //    return;
            //}
            //KiemKho(dt1, _barcode);
        }

        private void KiemKho(DataTable dt1, ArrayList _barcode)
        {
            DataTable dtRs = new DataTable();
            
            dtRs = getStructData();
            for (int i = 0; i < _barcode.Count; i++)
            {
                CBarcode cbtmp=new CBarcode();
                cbtmp = (CBarcode)_barcode[i];
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    if (cbtmp.BarCode == dt1.Rows[j]["ProductBarcode"].ToString())
                    {
                        dtRs.Rows.Add(dt1.Rows[j]["ProductBarcode"].ToString(), (Image)getImage(ImageNameCheck_OK_Green));
                    }
                    else 
                    {
                        dtRs.Rows.Add(i + j, dt1.Rows[j]["ProductBarcode"].ToString(), "2009/08/11", (Image)getImage(ImageNameCheck_OK_Red));
                    }
                }
                
            }
            //dataGridView1.DataSource = dtRs;
        }

        private DataSet LoadData()
        {
            UTLDB.UTLDB dbAccessor = new UTLDB.UTLDB();
            return dbAccessor.GetDataByPro("sp_KiemKho"); 


        }

        private DataTable getStructData()
        {
            DataTable rs = new DataTable();
            rs.Columns.Add("STT", typeof(int));
            rs.Columns.Add("BarCode",typeof(string));
            rs.Columns.Add("DateCreate",typeof(string));
            rs.Columns.Add("State",typeof(Image));
            rs.Columns["STT"].AutoIncrement = true;
            rs.Columns["STT"].AutoIncrementStep = 1;
            rs.Columns["STT"].AutoIncrementSeed = 1;
            return rs;
        }

        protected ArrayList FilterDuplicatRecord(ArrayList arr)
        {
            ArrayList arrRs = new ArrayList();
            CBarcode tmp = new CBarcode();
            for (int i = 0; i < arr.Count; i++)
            {
                tmp = (CBarcode)arr[i];
                AddToArrList(tmp, ref arrRs);
            }
            return arrRs;
        }

        private void AddToArrList(CBarcode valueAdd, ref ArrayList arrRs)
        {
            for (int i = 0; i < arrRs.Count; i++)
            {
                CBarcode a=new CBarcode();
                if (a.Compare(valueAdd,(CBarcode)arrRs[i]))
                {
                    return;
                }

            }
            arrRs.Add(valueAdd);
        }
        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog().Equals(DialogResult.OK))
            {
                txtPathFile.Text = openFileDialog1.FileName;
            }
        }

        private void txtPathFile_Click(object sender, EventArgs e)
        {
            btnChooseFile_Click(sender, e);
        }

        private void lstBarcode_BindingContextChanged(object sender, EventArgs e)
        {
           // lstBarcode.Cols["BarCodeDate"].Caption = "Ngày Tháng";
           // lstBarcode.Cols["BarCode"].Caption = "Mã Vạch";
           // lstBarcode.Cols["BarCodeNumber"].Caption = "Số Lượng";
        }

    }

}
